package com.aysafety.iotdataplatformserver.listener;

import com.alibaba.fastjson.JSON;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;

@Component
public class DataListener {

    @Autowired
    ErrorListener errorListener;

    @Autowired
    MicroseismListener microseismListener;

    @Autowired
    NaturalGasListener naturalGasListener;

    @Autowired
    HydraulicSupportListener hydraulicSupportListener;

    @Autowired
    GeosoundListener geosoundListener;

    @Autowired
    AcousticEmissionIntensityListener acousticEmissionIntensityListener;

    @Autowired
    CurrentBreakerListener currentBreakerListener;

    @Autowired
    ElectromagneticIntensityListener electromagneticIntensityListener;

    @Autowired
    ElectromagneticPulseListener electromagneticPulseListener;

    @Autowired
    GasListener gasListener;

    @Autowired
    SonicPulseListener sonicPulseListener;

    @Autowired
    WindsListener windsListener;

    private Logger logger = LoggerFactory.getLogger(getClass());

    public DataListener() {

    }

    private String getClientIp(ChannelHandlerContext ctx) {
        InetSocketAddress insocket = (InetSocketAddress) ctx.channel().remoteAddress();
        String clientIp = insocket.getAddress().getHostAddress();
        return clientIp;
    }

    @Async
    public void distribute(Object msg, ChannelHandlerContext ctx) throws Exception {
        String JSONMessage = msg.toString();
        Map<String, Object> result = JSON.parseObject(JSONMessage, Map.class);
        logger.info("distribute,result:"+result);
        if (result.containsKey("type")) {
            if ("Error".equals(result.get("type").toString())) {
                errorListener.start(result);
            } else if ("Microseism".equals(result.get("type").toString())) {
                microseismListener.start(result);
            } else if ("NaturalGas".equals(result.get("type").toString())) {
                //天然气
                //naturalGasListener.start(result);
            } else if ("hydraulicSupport".equals(result.get("type").toString())) {
                hydraulicSupportListener.start(result);
            } else if ("Geosound".equals(result.get("type").toString())) {
                geosoundListener.start(result);
            } else if ("AcousticEmissionIntensity".equals(result.get("type").toString())) {
                acousticEmissionIntensityListener.start(result);
            } else if ("CurrentBreaker".equals(result.get("type").toString())) {
                currentBreakerListener.start(result);
            } else if ("ElectromagneticIntensity".equals(result.get("type").toString())) {
                electromagneticIntensityListener.start(result);
            } else if ("ElectromagneticPulse".equals(result.get("type").toString())) {
                electromagneticPulseListener.start(result);
            } else if ("Gas".equals(result.get("type").toString())) {
                gasListener.start(result);
            } else if ("SonicPulse".equals(result.get("type").toString())) {
                sonicPulseListener.start(result);
            } else if ("Winds".equals(result.get("type").toString())) {
                windsListener.start(result);
            }
        } else {
            logger.info("通用报文格式错误");
        }
    }

    public void connection(ChannelHandlerContext ctx) {
        Map<String, Object> result = new HashMap<String, Object>();
        this.getClientIp(ctx);
    }

    public void close(ChannelHandlerContext ctx) {
        Map<String, Object> result = new HashMap<String, Object>();
        this.getClientIp(ctx);
    }
}
